#include "Player.h"
#include <iostream>

Player::Player()
{
}


Player::~Player()
{
}

void Player::initialize(sf::RenderWindow* window)
{
	m_renderWindow = window;
	isFainted = false;

	m_mainChar_pistol_tex = new sf::Texture();
	m_mainChar_rifle_tex = new sf::Texture();
	m_mainChar_shotgun_tex = new sf::Texture();

	m_mainChar_pistol_tex->loadFromFile("bilder/Player/Main_Char_Pistol.png");
	m_mainChar_rifle_tex->loadFromFile("bilder/Player/Main_Char_Rifle.png");
	m_mainChar_shotgun_tex->loadFromFile("bilder/Player/Main_Char_Shotgun.png");

	m_mainChar_pistol_sprite = new sf::Sprite();
	m_mainChar_rifle_sprite = new sf::Sprite();
	m_mainChar_shotgun_sprite = new sf::Sprite();

	m_mainChar_pistol_sprite->setTexture(*m_mainChar_pistol_tex);
	m_mainChar_rifle_sprite->setTexture(*m_mainChar_rifle_tex);
	m_mainChar_shotgun_sprite->setTexture(*m_mainChar_shotgun_tex);

	m_mainChar_pistol_sprite->setOrigin(28.5f, 25);
	m_mainChar_rifle_sprite->setOrigin(28.5f, 25);
	m_mainChar_shotgun_sprite->setOrigin(28.5f, 25);

	m_animation = new Animation();
	m_pistol_fire_anim = new Animation();
	m_rifle_fire_anim = new Animation();
	m_shotgun_fire_anim = new Animation();
	m_pistol_reload_anim = new Animation();
	m_shotgun_reload_anim = new Animation();
	m_rifle_reload_anim = new Animation();

	m_animation->Initialize("bilder/Player/WalkAnimationMainChar.png", sf::Vector2f(57, 50), 7, 12);
	m_pistol_fire_anim->Initialize("bilder/Player/Pistol_Fire.png", sf::Vector2f(92, 50), 3, 24);
	m_rifle_fire_anim->Initialize("bilder/Player/Rifle_Fire.png", sf::Vector2f(87.25f, 42), 4, 24);
	m_shotgun_fire_anim->Initialize("bilder/Player/Shotgun_Fire.png", sf::Vector2f(101.75f, 42), 4, 12);
	m_pistol_reload_anim->Initialize("bilder/Player/Pistol_reload.png", sf::Vector2f(57.0f, 50.0f), 10, 10);
	m_shotgun_reload_anim->Initialize("bilder/Player/Shotgun_reload.png", sf::Vector2f(67,50), 6, 12);
	m_shotgun_reload_anim->Initialize("bilder/Player/Rifle_reload.png", sf::Vector2f(67, 50), 8, 4);

	m_legs = m_animation->GetSprite();
	m_legs->setOrigin(28.5f, 25.0f);

	m_pistol_fire_sprite = m_pistol_fire_anim->GetSprite();
	m_pistol_fire_sprite->setOrigin(28.5f, 25);

	m_rifle_fire_sprite = m_rifle_fire_anim->GetSprite();
	m_rifle_fire_sprite->setOrigin(28.5f, 21);

	m_shotgun_fire_sprite = m_shotgun_fire_anim->GetSprite();
	m_shotgun_fire_sprite->setOrigin(28.5f, 21);

	m_playerSprite = new sf::Sprite();
	m_playerSprite->setTexture(*m_mainChar_pistol_tex);
	m_playerSprite->setPosition(1100.0f, 770.0f);
	
	m_clock = new sf::Clock();
	m_playerHealth = 100;
	shipHealth = 2000;

	isWalking = false;

	pistol_fire_clock = new sf::Clock();
	rifle_fire_clock = new sf::Clock();
	shotgun_fire_clock = new sf::Clock();

	firePistol = false;
	fireRifle = false;
	fireShotgun = false;

	measurePistolTime = false;
	measureRifleTime = false;
	measureShotgunTime = false;
}

void Player::update()
{
	
	timeSinceLastFrame = m_clock->restart();
	

	m_mainChar_pistol_sprite->setPosition(m_playerSprite->getPosition());
	m_mainChar_rifle_sprite->setPosition(m_playerSprite->getPosition());
	m_mainChar_shotgun_sprite->setPosition(m_playerSprite->getPosition());

	
	m_rifle_fire_sprite->setPosition(m_playerSprite->getPosition());
	m_shotgun_fire_sprite->setPosition(m_playerSprite->getPosition());
	
	if(equipPistol == true)
	{
		m_playerSprite = m_mainChar_pistol_sprite;
	}
	if(equipMachinegun == true)
	{
		m_playerSprite = m_mainChar_rifle_sprite;
	}
	if(equipShotgun == true)
	{
		m_playerSprite = m_mainChar_shotgun_sprite;
	}

	// Play walking animation
	if(isWalking == true)
	{
		m_legs->setPosition(m_playerSprite->getPosition());
		m_legs->setRotation(m_playerSprite->getRotation() + 180);
		m_renderWindow->draw(*m_legs);
		m_animation->Update(timeSinceLastFrame.asSeconds());
	}

	// Play pistol fire animation
	if(firePistol == true)
	{
		if(measurePistolTime == false)
		{
			pistol_fire_clock->restart();
			measurePistolTime = true;
			
		}
		else
		{
			pistol_fire_timer += pistol_fire_clock->restart();
			
		}
		m_pistol_fire_sprite->setPosition(m_playerSprite->getPosition());
		m_pistol_fire_sprite->setRotation(m_playerSprite->getRotation());

		m_pistol_fire_anim->Update(timeSinceLastFrame.asSeconds());
		m_renderWindow->draw(*m_pistol_fire_sprite);
		if(pistol_fire_timer.asSeconds() > m_pistol_fire_anim->m_updateRate * m_pistol_fire_anim->m_cellCount)
		{
			pistol_fire_timer -= pistol_fire_timer;
			firePistol = false;
			measurePistolTime = false;
		}
	}

	// Play rifle fire animation
	if(fireRifle == true)
	{
		if(measureRifleTime == false)
		{
			rifle_fire_clock->restart();
			measureRifleTime = true;
			
		}
		else
		{
			rifle_fire_timer += rifle_fire_clock->restart();
			
		}
		m_rifle_fire_sprite->setPosition(m_playerSprite->getPosition());
		m_rifle_fire_sprite->setRotation(m_playerSprite->getRotation());

		m_rifle_fire_anim->Update(timeSinceLastFrame.asSeconds());
		m_renderWindow->draw(*m_rifle_fire_sprite);
		if(rifle_fire_timer.asSeconds() > m_rifle_fire_anim->m_updateRate * m_rifle_fire_anim->m_cellCount)
		{
			rifle_fire_timer -= rifle_fire_timer;
			fireRifle = false;
			measureRifleTime = false;
		}
	}

	// Play rifle fire animation
	if(fireShotgun == true)
	{
		if(measureShotgunTime == false)
		{
			shotgun_fire_clock->restart();
			measureShotgunTime = true;
			
		}
		else
		{
			shotgun_fire_timer += shotgun_fire_clock->restart();
			
		}
		m_shotgun_fire_sprite->setPosition(m_playerSprite->getPosition());
		m_shotgun_fire_sprite->setRotation(m_playerSprite->getRotation());

		m_shotgun_fire_anim->Update(timeSinceLastFrame.asSeconds());
		m_renderWindow->draw(*m_shotgun_fire_sprite);
		if(shotgun_fire_timer.asSeconds() > m_shotgun_fire_anim->m_updateRate * m_shotgun_fire_anim->m_cellCount)
		{
			shotgun_fire_timer -= shotgun_fire_timer;
			fireShotgun = false;
			measureShotgunTime = false;
		}
	}

	// Draw the normal sprite if not fireing
	if(firePistol == false && fireRifle == false && fireShotgun == false && isFainted == false)
	{
		m_renderWindow->draw(*m_playerSprite);
	}
	
	

	isWalking = false;

	

	
	if(m_playerSprite->getPosition().x < 0 + 30)
	{
		m_playerSprite->setPosition(0 + 30, m_playerSprite->getPosition().y);
	}
	if(m_playerSprite->getPosition().x > 2560 - 30)
	{
		m_playerSprite->setPosition(2560 - 30, m_playerSprite->getPosition().y);
	}
	if(m_playerSprite->getPosition().y < 0 + 30)
	{
		m_playerSprite->setPosition(m_playerSprite->getPosition().x, 0 + 30);
	}
	if(m_playerSprite->getPosition().y > 1440 - 30)
	{
		m_playerSprite->setPosition(m_playerSprite->getPosition().x, 1440 - 30);
	}

	if(m_playerHealth < 0)
	{
		m_playerHealth = 0;
	}
	
}

void Player::cleanup()
{
	delete m_playerSprite;
	
}

void Player::move(float xDir, float yDir)
{
	float speedX = xDir * 100.0f * timeSinceLastFrame.asSeconds();
	float speedY = yDir * 100.0f * timeSinceLastFrame.asSeconds();
	
	m_playerSprite->move(speedX, speedY);

	isWalking = true;
}